package com.wuhongyu.service.impl;

import com.wuhongyu.base.BaseServiceImpl;
import com.wuhongyu.entity.Plan;
import com.wuhongyu.repository.PlanDao;
import com.wuhongyu.repository.ReferDao;
import com.wuhongyu.base.BaseService;
import com.wuhongyu.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("planService")
public class PlanServiceImpl extends BaseServiceImpl<Plan> implements PlanService {

    @Autowired
    private ReferDao referDao;
    private PlanDao planDao;

    @Resource
    public void setDao(PlanDao planDao) {
        super.setBaseDao(planDao);
        this.planDao = planDao;
    }

    @Override
    public List<Plan> findByRefer(Integer referid) {
        return planDao.findByReferEquals(referDao.findById(referid).get());
    }

    /**
     * 好烦呀 这jpa每次保存的时候一堆破事,头都大了
     *
     * @param plan
     */
    @Override
    public void save(Plan plan) {

        //保存的时候必然有referid
        //如果前端没传日期就赋值
        if (plan.getCreatetime() == null)
            plan.setCreatetime(new Date());
        plan.setRefer(referDao.findById(plan.getRefer().getId()).get());
        planDao.saveAndFlush(plan);

    }

}